package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 章节下面的小节
 * 课程小节
 * </p>
 *
 * @author testjava
 * @since 2023-03-05
 */
@RestController
@RequestMapping("/eduservice/eduVideo")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;


    /**
     * 添加小节
     * @param eduVideo
     * @return
     */
    @PostMapping("/addEduVideo")
    public R addEduVideo(@RequestBody EduVideo eduVideo)
    {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    /**
     * 删除小节
     * 删除小节的时候删除视频
     * @param videoId
     * @return
     */
    @DeleteMapping("/deleteEduVideo/{videoId}")
    public R deleteEduVideo(@PathVariable String videoId)
    {
        //先删除视频，再删除小节才行
        EduVideo video = eduVideoService.getById(videoId);
        vodClient.removeVideo(video.getVideoSourceId());
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    /**
     * 修改小节
     * @param eduVideo
     * @return
     */
    @PutMapping("/updateEduVideo")
    public R updateEduVideo(@RequestBody EduVideo eduVideo)
    {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    /**
     * 得到小节相关信息进行信息回显
     * @param videoId
     * @return
     */
    @GetMapping("/getEduVideo/{videoId}")
    public R getEduVideo(@PathVariable String videoId)
    {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("eduVideo",eduVideo);

    }

}

